package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.MyAddrAdapter;
import com.xgkj.diyiketang.data.AddressInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.AddressListTask;
import com.xgkj.diyiketang.task.MyOrderTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MyAddressActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener, PullToRefreshListView.OnLoadDataListener, View.OnClickListener {
    private static final String TAG = MyAddressActivity.class.getSimpleName();
    private Context mContext;
    private PullToRefreshListView mListView;
    private MyAddrAdapter mAdapter;
    public int pageNo = 1;
    private int size = 10;
    private TextView mBack;
    private NoDataUtil mNoData;
    private Button createBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.myaddress);
        mContext = this;
        initView();
    }

    private void initView() {
        mBack = (TextView) findViewById(R.id.back);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mListView = (PullToRefreshListView) findViewById(R.id.addrlist);
        mListView.init(mContext, true);
        mAdapter = new MyAddrAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListView.getHeaderViewsCount() > 0) {
                    position--;
                }
                if (position < 0 || position >= mAdapter.getCount()) return;
                AddressInfo info = (AddressInfo) mAdapter.getItem(position);
                Intent intent = new Intent();
                intent.putExtra("NAME", info.getAddressUser());
                intent.putExtra("PHONE", info.getMobile());
                intent.putExtra("ADDR", info.getAddress());

                //设置返回数据
                MyAddressActivity.this.setResult(RESULT_OK, intent);
                //关闭Activity
                MyAddressActivity.this.finish();
            }
        });
//        mListView.doRefresh();
        RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext, contentLayout);
        createBtn = (Button) findViewById(R.id.new_addr);
        createBtn.setOnClickListener(this);

    }

    public void getMyOrderList() {
        if (pageNo == 0) {
            mListView.setNoNextPage();
            return;
        }
        AddressListTask task = new AddressListTask(size, pageNo);
        task.addListener(myorderListener);
        getEatShowApp().getTaskManager().addTask(task);
    }

    private TaskEventListener myorderListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            if (pageNo == 1) {
                mListView.onRefreshComplete();
            } else {
                mListView.onLoadDataComplete(false);
            }
            super.handleTaskError(taskId, errorCode, errorMsg);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (pageNo == 1) {
                mListView.onRefreshComplete();
                mListView.enableLoadMore();
            } else {
                mListView.onLoadDataComplete(true);
            }
            if (data != null) {
                List<AddressInfo> list = new ArrayList<AddressInfo>();
                list = (List<AddressInfo>) data;
                if (pageNo == 1) {
                    mAdapter.clearData();
                }
                mAdapter.updateData(list);
            }
            pageNo = result;

            if (mAdapter != null && mAdapter.getCount() == 0) {
                mNoData.ListViewEmpty(mListView, null, null, -1);
            } else {
                mNoData.ListViewNoEmpty(mListView);
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        mListView.doRefresh();
    }

//    public void onHiddenChanged(boolean hidden) {
//        onHiddenChanged(hidden);
//        if(!hidden){
//            if(mAdapter != null && mAdapter.getCount() == 0){
//                mListView.doRefresh();
//            }
//
//        }
//    }


    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getMyOrderList();
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        getMyOrderList();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_addr:
                Intent intent = new Intent(mContext, NewAddrActivity.class);
                startActivity(intent);
                break;
        }
    }
}
